package com;

import com.fz.entity.Book;
import com.fz.entity.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import org.junit.Test;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by Z-C-Y on 2018-03-01.
 */
public class Demo {
    @Test
    public void aa() {
        /*插入一条信息*/
        Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
        SessionFactory sf = cfg.buildSessionFactory();
        Session session = sf.openSession();
        Transaction t = session.beginTransaction();
        Student stu = new Student();
        stu.setName("李四四");
        stu.setAge(18);
        stu.setMoney(new BigDecimal(1111));
        stu.setBirthday(new Date());
        stu.setInfo("你是好学生");
        session.save(stu);
        t.commit();
        session.close();/*关闭资源*/
    }

    @Test
    public void bb() {
        /*查询一个学生*/
        Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
        SessionFactory sf = cfg.buildSessionFactory();
        Session session = sf.openSession();
        Transaction t = session.beginTransaction();
        /*load()方法用的时候才数据库中查--是懒惰方法*/
        //  Student stu = session.load(Student.class,3);
        // System.out.println(stu);
        /*效率低*/
        // List<Student> list = session.createQuery("from student").list();
        //  for(Student stu:list){
        //    System.out.println(stu);
        // }

        /*根据姓名进行查询*/
        List<String> li = session.createQuery("select name from student").list();
        for (String stu1 : li) {
            System.out.println(stu1);
        }

        /*使用数组的形式进行显示*/
        List<Object[]> list4 = session.createQuery("select name,age from student ").list();
        for (Object[] o : list4) {
            System.out.println(o[0]);
            System.out.println(o[1]);

        }

        /*分页查询*/
        //分页查询
//        Query query = session.createQuery("from fz_book");
//        int currpage = 2;
//        int pagesize = 2;
//        query.setFirstResult(currpage*pagesize-pagesize);
//        query.setMaxResults(pagesize);
//        List<Book> list = query.list();
//        for(Book b : list){
//            System.out.println(b);
//        }


      /* 根据name查询*/
       /* Query query = session.createQuery("delete from com.fz.entity.Book where book_name:n");
         query.setParameter("n","张三");
         query.executeUpdate();

*/
        Query query = session.createQuery("select name from student where id = 5");
        String name = String.valueOf(query.uniqueResult());
        System.out.println(name);

        /*根据id进行修改数据*/
        Query query3= session.createQuery("update student set age=:aa,money=:money where id=:id");
        query3.setParameter("money",new BigDecimal(1000));
        query3.setParameter("aa",18);
        query3.setParameter("id",3);
        query3.executeUpdate();


     /*查询数据库中的数据条数*/
        Query query1 = session.createQuery("select count(*) from student");
        int rescordcount = Integer.parseInt(query1.uniqueResult().toString());
        System.out.println(rescordcount);

        t.commit();
        sf.close();

    }

    @Test
    public void book() {
        Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
        Session session = cfg.buildSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        Book book = new Book();
        book.setName("小明");
        session.save(book);
        t.commit();
        session.close();


    }


}
